package com.juyu.miliao.api.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.domain.dto.IntegralLevelAndTitleDTO;
import com.juyu.miliao.api.domain.vo.UserSortVo;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.IntegralLevelInfoService;
import com.juyu.miliao.api.service.OpenApiService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.UserSortService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.common.exception.IllegalParamException;

/**
 * 发现—魅力榜、贡献榜、时长榜(日榜、周榜、总榜、昨日榜)：
 *
 * @author wbf
 */
@RestController()
@RequestMapping("userSort")
public class UserSortController {

	@Resource
	private UserSortService userSortService;
	@Resource
	private OpenApiService openApiService;
	@Resource
	private AppKeyService appkeyService;
	@Resource
	private IntegralLevelInfoService integralLevelInfoService;

//    /**
//	 *
//	 * @param type 1:魅力榜 2:贡献榜 3:时长榜
//	 * @param day 1:日榜 2:周榜 3:总榜 4:昨日榜
//	 * @param page 页号
//	 * @param pageSize 每页显示条数
//	 * @return 该功能已废弃
//	 */
//	@GetMapping("/{type:^[1-9][0-9]*$}/{day:^[1-9][0-9]*$}/{page}-{pageSize}")
//	@JsonView(JacksonView.Follow.class)
//	public List<User> getUserList(HttpServletRequest request,Authentication token,
//								  @PathVariable("type") Integer type, @PathVariable("day") Integer day,
//								  @PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
//		CommonHead com = VerifyUtil.verifyCommonHead(request);
//		//是否正在审核
//		OpenApiStatus ops = openApiService.getOpenApi(com);
//		if (type==1 && ops != null && ops.getForwardAudit() && ops.getBackAudit()) {
//			String jsonStr = appkeyService.getAppKeyByKey("hostOnLineList").getValue();
//			return JSONArray.parseArray(jsonStr, User.class);
//		}else{
//			Subject subject = (Subject) token.getPrincipal();
//			long tokenUserId = subject.getId();
//			if (type == 0)
//				throw new IllegalParamException("参数错误：type不能为0");
//			if (day == 0)
//				throw new IllegalParamException("参数错误：day不能为0");
//			if (page <= 0) {
//				page = 1;
//			}
//			if (pageSize <= 0) {
//				pageSize = 20;
//			}
//			return userSortService.getUserSort(tokenUserId,type, day, page, pageSize);
//		}
//
//	}


	/**
	 * @param type 1:时长榜 2:钻石榜
	 * @param day 1:昨日 2:今日 3:本周 4:上周
	 * @param page 页号
	 * @param pageSize 每页显示条数
	 * @return
	 */
	@GetMapping("/host/{type:^[1-9][0-9]*$}/{day:^[1-9][0-9]*$}/{page}-{pageSize}")
	public List<UserSortVo> getHostUserList(HttpServletRequest request, Authentication token,
											@PathVariable("type") Integer type, @PathVariable("day") Integer day,
											@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		CommonHead com = VerifyUtil.verifyCommonHead(request);
		//是否正在审核
		OpenApiStatus ops = openApiService.getOpenApi(com);
		if (type==1 && ops != null && ops.getForwardAudit() && ops.getBackAudit()) {
			String jsonStr = appkeyService.getAppKeyByKey("hostOnLineList").getValue();
//			List<UserSortVo> list = new ArrayList<>();
//			 List<User> userList = JSONArray.parseArray(jsonStr, User.class);
//			for (User user :userList) {
//				IntegralLevelAndTitleDTO userIntegralLevel = integralLevelInfoService.getUserIntegralLevel(user.getIntegralSum());
//				UserSortVo userSortVo = new UserSortVo();
//				userSortVo.setUserId(user.getUserId());
//				userSortVo.setNiceName(user.getNickName());
//				userSortVo.setIcon(user.getIcon());
//				userSortVo.setLevel(userIntegralLevel.getIntegralGrade());
//
//			}
			//JSONArray.parseArray(jsonStr, User.class)
			return new ArrayList<>();
		}else{
			Subject subject = (Subject) token.getPrincipal();
			long tokenUserId = subject.getId();
			if (type == 0)
				throw new IllegalParamException("参数错误：type不能为0");
			if (day == 0)
				throw new IllegalParamException("参数错误：day不能为0");
			if (page <= 0) {
				page = 1;
			}
			if (pageSize <= 0) {
				pageSize = 20;
			}
			return userSortService.getHostUserSort(tokenUserId,type, day, page, pageSize);
		}

	}
// 已废弃
//	@GetMapping("/mySort/{type:^[1-9][0-9]*$}/{day:^[1-9][0-9]*$}")
//	public JSONObject getMySort(Authentication token, @PathVariable("type") Integer type,
//								@PathVariable("day") Integer day){
//		Subject subject = (Subject) token.getPrincipal();
//		long tokenUserId = subject.getId();
//		JSONObject json = new JSONObject();
//		json.put("sort",userSortService.getMySort(tokenUserId,type,day));
//		return json;
//	}

	/**
	 * 排行榜中  我的排名
	 * @param token
	 * @param type
	 * @param day
	 * @return
	 */
	@GetMapping("/myHostSort/{type:^[1-9][0-9]*$}/{day:^[1-9][0-9]*$}")
	public JSONObject getMyHostSort(Authentication token, @PathVariable("type") Integer type,
								@PathVariable("day") Integer day){
		Subject subject = (Subject) token.getPrincipal();
		long tokenUserId = subject.getId();
		JSONObject json = new JSONObject();
		json.put("sort",userSortService.getMyHostSort(tokenUserId,type,day));
		return json;
	}
}
